import datetime
import calendar
import requests
import json


def mallsession():
    '''
    猫酷系统权限session
    '''
    url = "https://passport.mp.mallcoo.cn/Home/PostLogin"
    payload = 'username=zjwy&password=98070d9ee4041b10f3966c17f55dd6e4&ispersistent=true&isCrossDomain=false'

    headers = {
        'Accept': '*/*',
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Cookie': 'acw_tc=707c9fdd16160454711422099e76749a07713087da20c86570474eb873b7a1'
    }
    session = requests.session()
    session.post(url, headers=headers, data=payload)
    return session


def login_session():
    '''
    客流系统权限session
    '''
    url = "http://218.3.159.249:8050/report/users/login"

    payload = "{\"loginName\":\"admin\",\"password\":\"Xck@900950\"}"
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        'Content-Type': 'application/json;charset=UTF-8'
    }
    session = requests.session()
    req = session.request("POST", url, headers=headers, data=payload)
    req.content.decode()
    req = json.loads(req.text)
    req = req['data']['atoken']
    return req


def get_kldata():
    '''
    获取当天实时进出客流
    :return:
    '''
    todays = datetime.date.today()
    # 客流数据
    url = "http://218.3.159.249:8050/report/report/trafficInAndOut/mall?_t=1616132275&orgIds=2&date=" + str(
        todays) + "&chartIds=327&option=TAB_DAY"

    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }

    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    kl_insum = klreq['data']['body']['trafficInAndOutTable']['series'][0]['data'][2]
    kl_outsum = klreq['data']['body']['trafficInAndOutTable']['series'][0]['data'][3]
    kl_zl = kl_insum - kl_outsum
    return kl_insum, kl_zl


def get_kl_trend():
    '''
    获取本月客流趋势
    :return:
    '''
    now = datetime.date.today()
    m_start = datetime.datetime(
        now.year, now.month, 1).strftime('%Y-%m-%d')
    m_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime(
        '%Y-%m-%d')
    url = 'http://218.3.159.249:8050/report/report/trafficInAndOut/mall?_t=1618104228&orgIds=2&startDate=' + str(
        m_start) + '&endDate=' + str(m_end) + '&chartIds=327'
    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    klreq = klreq['data']['body']['trafficInAndOutTable']['series']
    date = []
    insum = []
    outsum = []
    for i in klreq:
        date.append(i['name'])
        insum.append(i['data'][2])
        outsum.append(i['data'][3])
    return date, insum, outsum


def get_Top10_kl():
    '''
    获取客流排名前10店铺及客流数据
    :return:
    '''
    today = datetime.datetime.now().strftime('%Y-%m-%d')
    url = 'http://218.3.159.249:8050/report/report/ranking/zone?_t=1618105680&orgIds=17,17,34,54,33,53,19,22,36,55,57,20,60,48,49,7,97,80,141,171,123,106,179,42,21,142,205,162,164,101,74,75,99,30,204,8,12,72,16,43,114,29,81,89,93,25,38,122,140,170,78,117,103,110,188,94,73,146,128,84,66,104,158,130,127,203,83,87,67,96,95,199,157,150,153,121,151,86,5,3,172,115,112,91,37,161,26,105,125,147,10,169,71,69,159,200,88,166,156,61,111,70,167,50,186,149,102,180,4,100,40,15,6,155,2,175,148,41,168,18,108,185,62,32,64,65,163,35,120,182,28,119,85,98,76,77,90,124,9,126,68,143,82,118,109,11,139,13,184,27,177,154,201,208,174,92,44,45,176,202,63,178,116,144,145,107,183,160,173,79,181,165,129,23,113,47,152&startDate=' + str(
        today) + '&endDate=' + str(today) + '&chartIds=303&kpiTypes=TRAFFIC'
    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    klreq = klreq['data']['body']['rankingList']['series']
    shop_name = []
    shop_kl = []
    for i in klreq[1:11]:
        shop_name.append(i['name'])
        shop_kl.append(i['data'])
    return shop_name, shop_kl


def get_m_kl():
    '''
    获取商场月总客流
    :return:
    '''
    now = datetime.date.today()
    m_start = datetime.datetime(
        now.year, now.month, 1).strftime('%Y-%m-%d')
    m_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime(
        '%Y-%m-%d')
    url = 'http://218.3.159.249:8050/report/report/basics/mall?_t=1618106642&orgIds=2&startDate=' + str(
        m_start) + '&endDate=' + str(m_end) + '&chartIds=366&kpiType=TRAFFIC&option=TAB_TABLE'
    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    klreq = klreq['data']['body']['trafficOfMonth']['series'][0]['data'][1]
    return klreq


def get_s_kl():
    '''
    获取店铺月总客流
    :return:
    '''
    now = datetime.date.today()
    m_start = datetime.datetime(
        now.year, now.month, 1).strftime('%Y-%m-%d')
    m_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime(
        '%Y-%m-%d')
    url = 'http://218.3.159.249:8050/report/report/basics/zone?_t=1618110373&orgIds=17,34,54,33,53,19,22,36,55,57,20,60,48,49,7,97,80,141,171,123,106,179,42,21,142,205,162,164,101,74,75,99,30,204,8,12,72,16,43,114,29,81,89,93,25,38,122,140,170,78,117,103,110,188,94,73,146,128,84,66,104,158,130,127,203,83,87,67,96,95,199,157,150,153,121,151,86,5,3,172,115,112,91,37,161,26,105,125,147,10,169,71,69,159,200,88,166,156,61,111,70,167,50,186,149,102,180,4,100,40,15,6,155,2,175,148,41,168,18,108,185,62,32,64,65,163,35,120,182,28,119,85,98,76,77,90,124,9,126,68,143,82,118,109,11,139,13,184,27,177,154,201,208,174,92,44,45,176,202,63,178,116,144,145,107,183,160,173,79,181,165,129,23,113,47,152&startDate=' + str(
        m_start) + '&endDate=' + str(m_end) + '&chartIds=366&kpiType=TRAFFIC&option=TAB_TABLE'
    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    klreq = sum(klreq['data']['body']['trafficOfMonth']['series'][0]['data'][1:])
    return klreq


def get_kl_Contemporaneous_period():
    '''
    获取昨日同时段客流数据
    :return:
    '''
    now = str(datetime.datetime.now().strftime('%H')) + ":00"
    today = datetime.datetime.now().strftime('%Y-%m-%d')
    url = 'http://218.3.159.249:8050/report/report/yoyOrMom/day/mall?_t=1618108646&orgIds=2&startDate=' + str(
        today) + '&endDate=' + str(today) + '&chartIds=243,244,249,328,329,330,331,332,333,&kpiType=TRAFFIC'
    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    klreq = klreq['data']['body']['DayAddYoY']['series']
    insum = []
    for i in klreq:
        if i['data'][0] == now:
            insum.append(i['data'][2])
    return insum[0]


def get_kl_period_piont():
    '''
    获取昨日同时段客流百分比
    :return:
    '''
    now = str(datetime.datetime.now().strftime('%H')) + ":00"
    today = datetime.datetime.now().strftime('%Y-%m-%d')
    url = 'http://218.3.159.249:8050/report/report/yoyOrMom/day/mall?_t=1618108646&orgIds=2&startDate=' + str(
        today) + '&endDate=' + str(today) + '&chartIds=243,244,249,328,329,330,331,332,333,&kpiType=TRAFFIC'
    payload = {}
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Authorization': login_session(),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    klreq = requests.get(url, headers=headers, data=payload)
    klreq.content.decode()
    klreq = json.loads(klreq.text)
    klreq = klreq['data']['body']['DayAddYoY']['series']
    insum = []
    for i in klreq:
        if i['data'][0] == now:
            insum.append(i['data'][3])
    return insum[0]


def get_kl_Travel_depth():
    '''
    计算月游逛深度
    :return:
    '''
    shop_kl = get_s_kl()
    mall_kl = get_m_kl()
    VisitDepth = shop_kl / mall_kl
    VisitDepth = float('%.2f' % VisitDepth)
    return VisitDepth
